package com.itheima.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.bean.Category;
import com.itheima.dto.DishDto;
import com.itheima.bean.Setmeal;
import com.itheima.dao.CategoryDao;
import com.itheima.exception.CustomException;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    //分类管理——分类删除
    @Override
    public int delete(long id) {
       //1.先去检查菜品的数据
        List<DishDto> dishList = dishService.findByCid(id,null);
        if(dishList != null && dishList.size() > 0){
            //表示有菜品数据不能删除
            throw new CustomException("该分类有关联的菜品数据，禁止删除！");
        }
        //2.检查套餐的数据
        List<Setmeal> setmealList = setmealService.findByCid(id);
        if(setmealList != null && setmealList.size()>0){
            //表示有套餐数据不能删除
            throw new CustomException("该分类有关联的套餐数据，禁止删除！");
        }
        //3.如以上情况不存在就进行删除分类。返货收影响的行数1(成功)，2.(失败)
        return getBaseMapper().deleteById(id);
    }
}
